package com.btpn.custody.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.btpn.infra.entity.AEntityLong;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Table(name = "M_AREA")
@NamedQueries({
	@NamedQuery(name  = "Area.findAll",
				query = "FROM Area a ORDER BY a.name"),
	@NamedQuery(name  = "Area.getByCode",
				query = "FROM Area a WHERE a.code = :areaCode"),
	@NamedQuery(name  = "Area.getByName",
				query = "FROM Area a WHERE a.name = :areaName"),
	@NamedQuery(name  = "Area.findInRegion",
				query = "FROM Area a WHERE a.region.id = :regionId ORDER BY a.name")
})
public class Area extends AEntityLong {
	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = -7638850514933620542L;
	
	public static final String NQ__FIND__ALL		= "Area.findAll";
	
	public static final String NQ__FIND__BY__CODE 	= "Area.getByCode";
	
	public static final String NQ__FIND__BY__NAME 	= "Area.getByName";
	
	public static final String NQ__FIND__IN_REGION	= "Area.findInRegion";
	
	/*=============================================== M E M B E R =============================================*/
	@Column(name = "AREA_CODE", unique=true)
	private String code;

	@Column(name = "AREA_NAME")
	private String name;
	
	@Column(name = "ALL_BRANCH", columnDefinition="bit default 0")	// can view all branch for user area jakarta
	private boolean jakarta;
	
	@ManyToOne()
	@JoinColumn(name="REGION_ID")
	private Region region;
	
	@OneToMany(cascade = {CascadeType.REFRESH}, mappedBy = "area")
	private List<Branch> branchs = new ArrayList<Branch>();
	
	@OneToMany(cascade = {CascadeType.REFRESH}, mappedBy = "area")
	private List<User> users = new ArrayList<User>();
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	public boolean isJakarta() {
		return jakarta;
	}

	public void setJakarta(boolean jakarta) {
		this.jakarta = jakarta;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Region getRegion() {
		return region;
	}
	
	public void setRegion(Region region) {
		this.region = region;
	}

	public List<Branch> getBranchs() {
		return branchs;
	}

	public void setBranchs(List<Branch> branchs) {
		this.branchs = branchs;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}
